package com.fho4565.commands;

import com.fho4565.main.Configs;
import com.fho4565.main.Utils;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraftforge.common.ForgeConfigSpec;

import static com.fho4565.commands.CommandRegister.dispatcher;

public class KeyBinding {
    public static void register() {
        registerKeyCommand(1);
        registerKeyCommand(2);
        registerKeyCommand(3);
        registerKeyCommand(4);
        registerKeyCommand(5);
        registerKeyCommand(6);
    }
public static ForgeConfigSpec.ConfigValue<String> getConfig(int i){
    return switch (i) {
        case 1 -> Configs.keycd1;
        case 2 -> Configs.keycd2;
        case 3 -> Configs.keycd3;
        case 4 -> Configs.keycd4;
        case 5 -> Configs.keycd5;
        case 6 -> Configs.keycd6;
        default -> new ForgeConfigSpec.Builder().define("null", "");
    };
}
    private static void registerKeyCommand(int key) {
        dispatcher.register(
                Commands.literal("keyCommands").requires(s -> s.hasPermission(2))
                        .then(Commands.literal("get")
                                .executes(context -> {
                                    for (int j = 1;j < 7 ; j++) {
                                        Utils.sendTCdFeedback(context, "mcd.com.fho4565.command.keycd.get",
                                                String.valueOf(j),
                                                getConfig(j).get());
                                    }
                                    return 1;
                                })
                                .then(Commands.literal(String.valueOf(key))
                                        .executes(context -> {
                                            Utils.sendTCdFeedback(context, "mcd.com.fho4565.command.keycd.get",
                                                    String.valueOf(key),
                                                    getConfig(key).get());
                                            return 1;
                                        })
                                )
                        )
                        .then(Commands.literal("set")
                                .then(Commands.literal(String.valueOf(key)).then(Commands.argument("command", StringArgumentType.string())
                                        .executes(context -> {
                                            getConfig(key).set(StringArgumentType.getString(context, "command"));
                                            getConfig(key).save();
                                            Utils.sendTCdFeedback(context, "mcd.com.fho4565.command.keycd.set",String.valueOf(key));
                                            return 1;
                                        })
                                )
                        )
        ));
    }
}